package com.hook3.pianke.Adapter;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hook3.pianke.R;
import com.hook3.pianke.activity.SuiPianActivity;
import com.hook3.pianke.view.CircleImageView;

import org.xutils.common.Callback;

import org.xutils.x;

import java.util.List;

/**
 * 碎片的adapter标签也可以引用
 * Created by chai on 2015/12/12.
 */
public class SuiPianAdapter extends BaseAdapter {
    private List<SuiPianActivity.ItemData> fragmentinfos;
    private Activity activity;


    public SuiPianAdapter(List fragmentinfos,Activity activity){
        this.fragmentinfos = fragmentinfos;
        this.activity = activity;

    }


    @Override
    public int getCount() {
        return fragmentinfos.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView = View.inflate(activity, R.layout.adapter_timeline_list,null);
            //用户信息
            viewHolder.fragment_iv_photo = (CircleImageView) convertView.findViewById(R.id.fragment_iv_photo);
            viewHolder.fragment_tv_author = (TextView) convertView.findViewById(R.id.fragment_tv_author);
            viewHolder.fragment_tv_timeup = (TextView) convertView.findViewById(R.id.fragment_tv_timeup);

            //发表得内容图片
            viewHolder.fragment_rl_music_pic = (RelativeLayout) convertView.findViewById(R.id.fragment_rl_music_pic);
            viewHolder.fragment_iv_music = (ImageView) convertView.findViewById(R.id.fragment_iv_music);
            //音乐链接
            viewHolder.fragment_music_play_bar = (RelativeLayout) convertView.findViewById(R.id.fragment_music_play_bar);

            //发表的话
            viewHolder.fragment_tv_content = (TextView) convertView.findViewById(R.id.fragment_tv_content);
            //tag的显示

            viewHolder.fragment_tag_info = (RelativeLayout) convertView.findViewById(R.id.fragment_tag_info);
            viewHolder.tv_adapter_tag_name = (TextView) convertView.findViewById(R.id.tv_adapter_tag_name);
            viewHolder.tv_adapter_tag_count = (TextView) convertView.findViewById(R.id.tv_adapter_tag_count);

            //评论与喜欢
            viewHolder.fragment_tv_comment = (TextView) convertView.findViewById(R.id.fragment_tv_comment);
            viewHolder.timeline_like = (ImageView) convertView.findViewById(R.id.timeline_like);
            viewHolder.fragment_tv_like = (TextView) convertView.findViewById(R.id.fragment_tv_like);
            convertView.setTag(viewHolder);

        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SuiPianActivity.ItemData item = fragmentinfos.get(position);
        ImageView imageView = new ImageView(activity);
        x.image().bind(imageView, item.userinfo.icon, new myim(viewHolder.fragment_iv_photo));
        viewHolder.fragment_tv_author.setText(item.userinfo.uname);


        viewHolder.fragment_tv_timeup.setText(item.addtime_f);

        if((item.coverimg).isEmpty()){
            viewHolder.fragment_rl_music_pic.setVisibility(View.GONE);
        }else{
            viewHolder.fragment_rl_music_pic.setVisibility(View.VISIBLE);
        }
        if((item.songid).isEmpty()){
            viewHolder.fragment_music_play_bar.setVisibility(View.GONE);
        }else{
            viewHolder.fragment_music_play_bar.setVisibility(View.VISIBLE);
        }



        x.image().bind(viewHolder.fragment_iv_music, item.coverimg);
        if(!(item.tag_info.tag).isEmpty()){
            viewHolder.fragment_tag_info.setVisibility(View.VISIBLE);
            viewHolder.tv_adapter_tag_name.setText("·"+item.tag_info.tag);
            viewHolder.tv_adapter_tag_count.setText("·"+item.tag_info.count);
        }else{
            viewHolder.fragment_tag_info.setVisibility(View.GONE);
        }

        viewHolder.fragment_tv_content.setText(item.content);
        viewHolder.fragment_tv_comment.setText(item.counterList.comment+"");
        viewHolder.timeline_like.setImageResource(R.mipmap.ic_heart_empty);
        viewHolder.fragment_tv_like.setText(item.counterList.like+"");

        return convertView;
    }
    class myim implements Callback.CommonCallback<Drawable>{
        CircleImageView fragment_iv_photo;
        public myim(CircleImageView fragment_iv_photo) {
            this.fragment_iv_photo=fragment_iv_photo;
        }


        @Override
        public void onSuccess(Drawable result) {
            fragment_iv_photo.setImageDrawable(result);

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    }
    static class ViewHolder{
        CircleImageView fragment_iv_photo;//用户头像
        TextView fragment_tv_author;//用户名
        TextView fragment_tv_timeup;//发表时间
        ImageView fragment_iv_music;//发表的图片
        RelativeLayout fragment_rl_music_pic;//根据判断是否显示这个页面
        RelativeLayout fragment_music_play_bar;//发表音乐界面

        RelativeLayout fragment_tag_info;
        TextView tv_adapter_tag_name;//tag name
        TextView tv_adapter_tag_count;//tag数量

        TextView fragment_tv_content;//发表的内容
        TextView fragment_tv_comment;//评论的条数
        ImageView timeline_like;//收藏图片的变化
        TextView fragment_tv_like;//被喜欢的数量
    }




}
